﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emergency4.Dal;
using Emergency4.From;
using Emergency4.Session;

namespace Emergency4.From
{
    /// <summary>
    /// 维修经理页面
    /// </summary>
    public partial class Admin : Form
    {
        public Admin()
        {
            InitializeComponent();
        }

        private void Admin_Load(object sender, EventArgs e)
        {
            List<EmergencyMaintenance> list = AdminDal.findAllEmergency();
            showAllInfo(list);
            dataGridView1.CellContentClick += new DataGridViewCellEventHandler(dataGridView1_CellContentClick);
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.SelectedCells.Count == 1 && e.RowIndex >= 0)
            {
                if (dataGridView1.SelectedCells[0].Value.ToString() == "Management Request")
                {
                    EmergencyMaintenanceRequestDetails rq = new EmergencyMaintenanceRequestDetails();
                    rq.id = int.Parse(dataGridView1.SelectedCells[0].OwningRow.Tag.ToString());
                    rq.Show();
                    rq.FormClosed += new FormClosedEventHandler(this_from);
                }
            }
        }

        private void this_from(object sender, FormClosedEventArgs e)
        {
            List<EmergencyMaintenance> list = AdminDal.findAllEmergency();
            showAllInfo(list);
        }

        string[] header = new string[] { "AssetSN", "AssetName", "Request Date", "Employee Full Name", "Department", "Priority Name" };
        private void showAllInfo(List<EmergencyMaintenance> emList)
        {
            dataGridView1.Rows.Clear();
            dataGridView1.Columns.Clear();

            dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            dataGridView1.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView1.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False;

            foreach (string item in header)
            {
                DataGridViewTextBoxColumn c = new DataGridViewTextBoxColumn();
                c.FillWeight = 500;
                c.Name = item;
                dataGridView1.Columns.Add(c);
            }
            DataGridViewLinkColumn c2 = new DataGridViewLinkColumn();
            c2.FillWeight = 500;
            c2.Name = "Operation";
            dataGridView1.Columns.Add(c2);
            for (int i = 0; i < emList.Count; i++)
            {
                dataGridView1.Rows.Add(emList[i].Asset.AssetSN, emList[i].Asset.AssetName, emList[i].EMReportDate, emList[i].Asset.Employee.FirstName + " " + emList[i].Asset.Employee.LastName, emList[i].Asset.DepartmentLocation.Location.Name, emList[i].Priority.Name, "Management Request");
                dataGridView1.Rows[i].Tag = emList[i].ID;
            }
        }
        }
}
